﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EntModApp.EntServices.IoC;
using Microsoft.Practices.Unity.Configuration;
using Microsoft.Practices.Unity;
using System.Configuration;

namespace EntModApp.EntServices.EntLibServices
{
  public class UnityServiceLocatorAdapter : IServiceLocator
  {
    private Microsoft.Practices.ServiceLocation.IServiceLocator unityServiceLocator;

    public UnityServiceLocatorAdapter(Microsoft.Practices.ServiceLocation.IServiceLocator unityServiceLocator)
    {
      if (unityServiceLocator == null)
        throw new ArgumentNullException("unityServiceLocator");
      this.unityServiceLocator = unityServiceLocator;
    }

    public UnityServiceLocatorAdapter(UnityContainer container)
      : this(new UnityServiceLocator(container))
    {
    }

    public UnityServiceLocatorAdapter()
    {
      var section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
      var container = new UnityContainer();
      section.Configure(container);
      unityServiceLocator = new UnityServiceLocator(container);
    }

    public IEnumerable<TService> GetAllInstances<TService>()
    {
      IEnumerable<TService> result = null;
      try
      {
        result = unityServiceLocator.GetAllInstances<TService>();
      }
      catch (Exception ex)
      {
        result = Enumerable.Empty<TService>();
      }
      return result;
    }

    public IEnumerable<object> GetAllInstances(Type serviceType)
    {
      IEnumerable<object> result = null;
      try
      {
        result = unityServiceLocator.GetAllInstances(serviceType);
      }
      catch (Exception ex)
      {
        result = Enumerable.Empty<object>();
      }
      return result;
    }

    public TService GetInstance<TService>()
    {
      TService result = default(TService);
      try
      {
        result = unityServiceLocator.GetInstance<TService>();
      }
      catch(Exception ex)
      {
        if (ex.GetType().FullName != "Microsoft.Practices.ServiceLocation.ActivationException")
          throw;
      }
      return result;
    }

    public TService GetInstance<TService>(string key)
    {
      TService result = default(TService);
      try
      {
        result = unityServiceLocator.GetInstance<TService>(key);
      }
      catch (Exception ex)
      {
        if (ex.GetType().FullName != "Microsoft.Practices.ServiceLocation.ActivationException")
          throw;
      }
      return result;
    }

    public object GetInstance(Type serviceType)
    {
      object result = null;
      try
      {
        result = unityServiceLocator.GetInstance(serviceType);
      }
      catch (Exception ex)
      {
        if (ex.GetType().FullName != "Microsoft.Practices.ServiceLocation.ActivationException")
          throw;
      }
      return result;
    }

    public object GetInstance(Type serviceType, string key)
    {
      object result = null;
      try
      {
        result = unityServiceLocator.GetInstance(serviceType, key);
      }
      catch (Exception ex)
      {
        if (ex.GetType().FullName != "Microsoft.Practices.ServiceLocation.ActivationException")
          throw;
      }
      return result;
    }
  }
}
